'use strict';
const mailer = require('nodemailer');

const defaultParams = {
  service: 'qq',
  auth: {
    user: '1947914887@qq.com', // generated ethereal user
    pass: 'bfohugrudpwvfche'  // generated ethereal password
  }
};
const config_qq = {
  service: 'qq',
  auth: {
    user: '1947914887@qq.com', // generated ethereal user
    pass: 'bfohugrudpwvfche'  // generated ethereal password
  }
};
const config_163 = {
  service: '163',
  auth: {
    user: 'cy_wangsy@163.com', // generated ethereal user
    pass: 'mouse123456'  // generated ethereal password
  }
};

class Mail {
  constructor(param) {
    this.opts = Object.assign({}, this.opts, defaultParams, param);
    this.transporter = null;

    this.init();
  }

  init() {
    this.transporter = mailer.createTransport(this.opts);
  }

  /**
   * 发送邮件
   * @param to  发送给谁
   * @param subject  主题
   * @param content 内容
   * @return {Promise}
   */
  sendMail(to, subject, content) {
    return new Promise((resolve, reject) => {
      let mailOption = Object.assign({}, {
        from: this.opts.sendUser || this.opts.auth.user, // sender address
        to: to,
        html: content,
        subject: subject
      });

      this.transporter.sendMail(mailOption, (error, info) => {
        if (error) {
          reject(error);
        } else {
          resolve(info);
        }
      });
    });
  }
}

let mail = new Mail();

module.exports = mail;
